package com.witmed.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.witmed.entity.Article;
import com.witmed.entity.Discuss;
import com.witmed.entity.UserInfo;
import com.witmed.service.DiscussService;
import com.witmed.service.impl.DiscussServiceImpl;

/**
 * Servlet implementation class DiscussServlet
 */
@WebServlet("/DiscussServlet")
@MultipartConfig
public class DiscussServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//注入业务层
	private DiscussService discussService =new DiscussServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DiscussServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op =request.getParameter("op");
		if("searchDiscuss".equals(op)) {
			doFindDiscuss(request,response);
		}else if ("insertDiscuss".equals(op)) {
			doInsertDiscuss(request,response);
		}else if ("insertReply".equals(op)) {
			doInsertReply(request,response);
		}else if ("deleteDiscuss".equals(op)) {
			doDeleteReply(request,response);
		}
	}
	
	
	private void doDeleteReply(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userId = request.getParameter("userId");
		String discussId = request.getParameter("discussId");
		boolean i = discussService.deleteDicussById(Integer.parseInt(discussId),Integer.parseInt(userId));
		ObjectMapper objectMapper =new ObjectMapper();
		String json="";
		if (i) {
			 json= objectMapper.writeValueAsString("success");
		}
		response.getWriter().print(json);
		
	}

	/**
	 * 新增评论
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
private void doInsertDiscuss(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String content = request.getParameter("content");
		String articleId = request.getParameter("articleId");
		Article article = new Article(Integer.parseInt(articleId), null, null, null, null);
		String userId = request.getParameter("userId");
		UserInfo user =new UserInfo(Integer.parseInt(userId), null, null, null, null, null,null, null, null);
		Date date  =new Date();
		SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String discussTime = simpleDateFormat.format(date);
		boolean i = discussService.insertToArticle(new Discuss(null, content, discussTime, user, article, null));
		ObjectMapper objectMapper =new ObjectMapper();
		String json = objectMapper.writeValueAsString("success");
		response.getWriter().print(json);
	}

/**
 * 新增评论
 * @param request
 * @param response
 * @throws IOException 
 */
private void doInsertReply(HttpServletRequest request, HttpServletResponse response) throws IOException {
	String content = request.getParameter("content");
	String articleId = request.getParameter("articleId");
	Article article = new Article(Integer.parseInt(articleId), null, null, null, null);
	String userId = request.getParameter("userId");
	UserInfo user =new UserInfo(Integer.parseInt(userId), null, null, null, null, null,null, null, null);
	String discussId = request.getParameter("discussId");
	Date date  =new Date();
	SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	String discussTime = simpleDateFormat.format(date);
	boolean i = discussService.insert(new Discuss(null, content, discussTime, user, article, new Discuss(Integer.parseInt(discussId), null, null, null, null, null)));
	ObjectMapper objectMapper =new ObjectMapper();
	String json = objectMapper.writeValueAsString("success");
	response.getWriter().print(json);
}

/**
 * 查询评论
 * @param request
 * @param response
 * @throws IOException 
 */
	private void doFindDiscuss(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("application/json;charset=utf-8");
		String articleId = request.getParameter("articleId");
		List<Discuss> list = discussService.findDicussBy(Integer.parseInt(articleId));
		ObjectMapper objectMapper =new ObjectMapper();
		String json = objectMapper.writeValueAsString(list);
		response.getWriter().print(json);
		
	}

}
